local L = HOMURA_GLOBALS.LANGUAGE 

STRINGS.NAMES.HOMURA_WORKDESK = L and "Thermal weapon workbench" or "武器工作台"
STRINGS.CHARACTERS.HOMURA_1.DESCRIBE.HOMURA_WORKDESK = L and "I can make weapons here." or "我可以制造热兵器了。"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.HOMURA_WORKDESK = L and "It looks modern!" or "这个研究站看起来好高级啊!"
STRINGS.RECIPE_DESC.HOMURA_WORKDESK = L and "Modern weapon technology!" or "研发现代兵器科技！"

STRINGS.NAMES.HOMURA_WRENCH = L and 'Tech wrench' or '升级扳手'
STRINGS.CHARACTERS.HOMURA_1.DESCRIBE.HOMURA_WRENCH = L and 'This gadget can upgrade my workdesk.' or '这个小工具可以让我的工作台能制作更多的武器'
STRINGS.CHARACTERS.GENERIC.DESCRIBE.HOMURA_WRENCH = L and 'A useful wrench.' or '一个精致的小扳手'
STRINGS.RECIPE_DESC.HOMURA_WRENCH = L and 'Improve the workdesk.' or '用来改良武器工作台'

require "prefabutil"

local function onhammered(inst, worker)
    if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then
        inst.components.burnable:Extinguish()
    end
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    if inst:GetLevel() == 1 then
    	fx:SetMaterial("wood")
    else
    	fx:SetMaterial("metal")
    end
    inst:Remove()
end

local function onsave(inst, data)
    if inst:HasTag("burnt") or (inst.components.burnable ~= nil and inst.components.burnable:IsBurning()) then
        data.burnt = true
    end
end

local function onload(inst, data)
    if data ~= nil and data.burnt then
        inst.components.burnable.onburnt(inst)
    end
    if inst.components.upgradeable and inst.SetLevel then
    	inst:SetLevel(inst.components.upgradeable.stage)
    end
end

local function onhit(inst, worker)
	--inst.AnimState:PlayAnimation("hit")
	--inst.AnimState:PushAnimation(inst.on and "proximity_loop" or "idle", true)
end

local function onupgradefn(inst)
	inst:SetLevel(2)
	SpawnPrefab('small_puff').Transform:SetPosition(inst.Transform:GetWorldPosition())
end

local function createmachine()
	local function onturnon(inst)
		inst.AnimState:PlayAnimation("loop_pre")
		inst.AnimState:PushAnimation("loop_loop")
		if inst:GetLevel() == 2 then
			inst.SoundEmitter:PlaySound('lw_homura/sfx/workdesk2_on', nil, 0.5)
			inst.Light:Enable(true)
    		inst.components.lighttweener:StartTween(nil, 2, nil, nil, nil, 0.2) 
		end
		inst.SoundEmitter:PlaySound("dontstarve/common/researchmachine_lvl1_idle_LP","idlesound")
	end

	local function onturnoff(inst)
	    inst.AnimState:PlayAnimation("loop_pst")
	    inst.AnimState:PushAnimation('idle',false)
	    if inst:GetLevel() == 2 then
	    	inst.SoundEmitter:PlaySound('lw_homura/sfx/workdesk2_off', nil, 0.6)
	    	inst.components.lighttweener:StartTween(nil, 0, nil, nil, nil, 0.2, function(inst)inst.Light:Enable(false)end)
	    end
		inst.SoundEmitter:KillSound("idlesound")
	end

	local assets = 
	{
		Asset("ANIM", "anim/homura_workdesk1.zip"),
		Asset("ANIM", "anim/homura_workdesk2.zip"),
		Asset("ATLAS","images/inventoryimages/homura_workdesk.xml"),
	}

	local function fn(Sim)
		local inst = CreateEntity()
		local trans = inst.entity:AddTransform()
		local anim = inst.entity:AddAnimState()
		local minimap = inst.entity:AddMiniMapEntity()
		local light = inst.entity:AddLight()
		inst.entity:AddSoundEmitter()
		inst.entity:AddNetwork()

		light:Enable(false)
    	light:SetRadius(.6)
    	light:SetFalloff(.5)
    	light:SetIntensity(.5)
    	light:SetColour(0.25,0.25,1)

		minimap:SetPriority( 5 )
		--minimap:SetIcon( name..".png" )--@--
	    
		MakeObstaclePhysics(inst, .4)

		local s = 1.2
		trans:SetScale(s,s,s)
	    
		anim:SetBank('homura_workdesk')
		anim:SetBuild('homura_workdesk')
		anim:PlayAnimation("idle")

		inst:AddTag("homura_prototyper")
        inst:AddTag("structure")

        MakeSnowCoveredPristine(inst)

        inst.entity:SetPristine()
        if not TheWorld.ismastersim then
        	return inst
    	end
		
		inst:AddComponent("inspectable")

    	inst:AddComponent("prototyper")
        inst.components.prototyper.onturnon = onturnon
        inst.components.prototyper.onturnoff = onturnoff
        inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.HOMURA_TECH_ONE

    	inst:AddComponent('lighttweener')
    	inst.components.lighttweener:StartTween(light, 1, .9, 0.9, {0.25,0.25,1}, 0, function(inst)inst.Light:Enable(false)end)

		inst:ListenForEvent( "onbuilt", function()
			--anim:PlayAnimation("place")
			--anim:PlayAnimation("loop_pre")
			--anim:PushAnimation("loop_loop", true)				
		end)

		inst:AddComponent("hauntable")
        inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)

		inst.SetLevel = function(inst,level)
			inst.AnimState:SetBuild('homura_workdesk'..level)
			inst.AnimState:SetBank('homura_workdesk'..level)
			if level == 2 then
				inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.HOMURA_TECH_THREE
			end
		end
		inst.GetLevel = function(inst) 
			return inst.components.upgradeable.stage 
		end
		inst:SetLevel(1)

		inst:AddComponent("lootdropper")
		inst:AddComponent("workable")
		inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
		inst.components.workable:SetWorkLeft(4)
		inst.components.workable:SetOnFinishCallback(onhammered)
		inst.components.workable:SetOnWorkCallback(onhit)

		inst:AddComponent("upgradeable")	
		inst.components.upgradeable.numstages = 2
		inst.components.upgradeable.upgradesperstage = 0
		inst.components.upgradeable.upgradetype = UPGRADETYPES.HOMURA_WORKDESK
		inst.components.upgradeable.onupgradefn = onupgradefn

		inst.OnSave = onsave
		inst.OnLoad = onload

	 	--MakeLargeBurnable(inst, nil, nil, true)
        --MakeLargePropagator(inst)

		MakeSnowCovered(inst, .01)
		return inst
	end
	return Prefab( "homura_workdesk", fn, assets)
end

local assets = 
{
    Asset("ANIM","anim/homura_wrench.zip"),
    Asset("ATLAS","images/inventoryimages/homura_wrench.xml"),
}

local function Wrench()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local net   = inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)
    MakeInventoryFloatable(inst, "med", 0.12, 0.7)

    anim:SetBank("homura_wrench")
    anim:SetBuild("homura_wrench")
    anim:PlayAnimation("anim")

    trans:SetScale(.8,.8,.8)

    inst:AddTag("irreplacable")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    MakeHauntableLaunch(inst)

    inst:AddComponent("inspectable")
    inst:AddComponent("upgrader")
    inst.components.upgrader.upgradetype = UPGRADETYPES.HOMURA_WORKDESK

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/homura_wrench.xml"

    return inst
end

return createmachine(), MakePlacer("homura_workdesk_placer", "homura_workdesk1", "homura_workdesk1", "idle"),
Prefab("homura_wrench", Wrench, assets)
